const mongoose = require("mongoose");
const axios = require("axios").default;
const Movie = require("./models/movie");

mongoose
    .connect("mongodb://localhost:27017/app-20111")
    .then(() => {
        console.log("数据库连接成功");
        loadMovies(); // 补充假数据
    })
    .catch((err) => console.log(err));

function loadMovies() {
    const movies = [];
    axios
        .get(
            "https://pcw-api.iqiyi.com/search/recommend/list?channel_id=1&data_type=1&mode=11&page_id=1&ret_num=48"
        )
        .then((res) => {
            res.data.data.list.forEach((m) => {
                console.log(m)
                let movie = {};
                movie.name = m.name;
                movie.story = m.description;
                movie.img = m.imageUrl;
                movie.playUrl = m.playUrl;
                movies.push(movie);
            });
            Movie.insertMany(movies).then(() => console.log("插入数据成功"));
        });
}
